package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.Order;
import com.project.entity.qo.OrderQo;
import com.project.entity.vo.OrderVo;
import com.project.exception.ServiceException;
import com.project.mapper.OrderMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.OrderService;
import com.project.utils.OrderUtils;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chen
 * @date 2022/3/29
 */
@SuppressWarnings("all")
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderUtils orderUtils;



    /**
     * 添加订单
     * @param orderQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addOrder(OrderQo orderQo) {
        Order order = new Order();
        BeanUtils.copyProperties(orderQo,order);
        //获取订单编号
        order.setOrderNumber(orderUtils.getOrderNumber());

        int insert = orderMapper.insert(order);
        if(insert == 0){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public ResponseResult<List<OrderVo>> selectOrder() {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.lambda().eq(Order::getIsDelete,1);
        List<Order> orders = orderMapper.selectList(qw);
        List<OrderVo> orderVoList = new ArrayList<>();
        for (Order o:orders) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(o,orderVo);
            orderVoList.add(orderVo);
        }
        if(ObjectUtils.isEmpty(orderVoList)){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,orderVoList);
    }

    /**
     * 根据条件查询
     * @param orderQo
     * @return
     */
    @Override
    public ResponseResult<List<OrderVo>> selectByOrder(OrderQo orderQo) {
        Order order = new Order();
        BeanUtils.copyProperties(orderQo,order);

        List<Order> orderList = orderMapper.selectByOrder(order);

        List<OrderVo> orderVoList = new ArrayList<>();

        for (Order o:orderList) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(o,orderVo);
            orderVoList.add(orderVo);
        }
        if(ObjectUtils.isEmpty(orderVoList)){
            throw  new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,orderVoList);
    }

    /**
     * 分页查询
     * @param current
     * @return
     */
    @Override
    public ResponseResult<List<OrderVo>> selectOrderPage(Integer current) {
        //传递当前页数和每页数据量
        Page<Order> page = new Page<>(current,3);
        //查询条件
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.lambda().eq(Order::getIsDelete,1);
        Page<Order> orderPage = orderMapper.selectPage(page, qw);
        if(ObjectUtils.isEmpty(orderPage)){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        //获取当前页数据
        List<Order> orderList = orderPage.getRecords();

        List<OrderVo> orderVoList = new ArrayList<>();
        for (Order o:orderList) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(o,orderVo);
            orderVoList.add(orderVo);
        }
        return ResponseResult.success(ResultCode.SUCCESS,orderVoList);
    }
}
